package com.lu.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.statistics.entity.MtWedding;
import com.lu.statistics.entity.enums.ProductTypeEnum;
import com.lu.statistics.entity.front.response.ProGroupResponse;
import com.lu.statistics.entity.front.response.WeddingResponse;
import com.lu.statistics.mapper.MtWeddingMapper;
import com.lu.statistics.service.GroupService;
import com.lu.statistics.service.WeddingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WeddingServiceImpl extends ServiceImpl<MtWeddingMapper, MtWedding> implements WeddingService, GroupService {
    @Autowired
    private MtWeddingMapper weddingMapper;

    @Override
    public IPage<MtWedding> queryPageWeddingList(String isAvailable, int current, int pageSize) {
        Page<MtWedding> page = new Page(current, pageSize);
        QueryWrapper<MtWedding> wrapper = new QueryWrapper<>();
        wrapper.eq("is_available", isAvailable);
        wrapper.orderBy(true, false, "create_time");
        return weddingMapper.selectPage(page, wrapper);
    }

    @Override
    public ProGroupResponse queryGroupList(int pageNo, int pageSize) {
        ProGroupResponse groupResponse = new ProGroupResponse();
        List<WeddingResponse> newList = new ArrayList<>();
        IPage page = queryPageWeddingList("LISTING", pageNo, pageSize);
        if (page.getTotal() > 0) {
            for (Object item : page.getRecords()) {
                MtWedding wedding = (MtWedding) item;
                WeddingResponse weddingResponse = new WeddingResponse();
                String url = "/api/file/previewPhoto?fileFullPath=" + wedding.getImgUrl();
                BeanUtils.copyProperties(wedding, weddingResponse);
                weddingResponse.setImgUrl(url);
                newList.add(weddingResponse);
            }
            groupResponse.setWeddingTotal(page.getTotal());
        }
        groupResponse.setWeddingList(newList);
        return groupResponse;
    }

    @Override
    public ProductTypeEnum getType() {
        return ProductTypeEnum.BANQUET;
    }
}
